
/* --------------------------------------
 * Copyright 2008 Jean-Matthieu BARBIER
 * --------------------------------------
 * This file is part of QLocatePC.
 *
 * QLocatePC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QLocatePC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QLocatePC.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QCoreApplication>
#include <QSettings>
#include <QtNetwork>
#include "setup.h"


QLocatePCSetupData::QLocatePCSetupData() : QSettings() {
	
}	

bool QLocatePCSetupData::check(QString* errormsg) {
  return true;
}

void QLocatePCSetupData::set() {
  setValue("locateUrl",url);
  setValue("PCIdentifier",identifier);
  setValue("ComputerName",computername);
  setValue("MoreInfo",moreinfo);
  if(daemoninterval<60000) daemoninterval=60000;
  setValue("TimerDelay",daemoninterval);
  setValue("RunDaemon",daemon);
}


void QLocatePCSetupData::load() {
  url = value("locateUrl","http://www.solidev.org/devel/QLocatePC/net/").toString();
  identifier = value("PCIdentifier","UNK").toString();
  computername = value("ComputerName","CNAME").toString();
  moreinfo = value("MoreInfo","Misc Data").toString();
  daemoninterval = value("TimerDelay","10000").toInt();
  daemon = value("RunDaemon","0").toBool();
}

void QLocatePCSetupData::setdefaults() {
  QNetworkInterface iface;
  QList<QNetworkInterface> ifacelist;
  ifacelist =  iface.allInterfaces();
  int i;
  url = "http://www.solidev.org/devel/QLocatePC/net/";
  QUuid uid;
  uid = QUuid::createUuid();
  identifier = uid.toString();
  computername = "TODO_COMPUTER_NAME";
  moreinfo = "";

  //-- Platform specific data
  #ifdef Q_WS_WIN
  QSettings mname("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet",
                            QSettings::NativeFormat);
  computername = mname.value("Control/ComputerName/ActiveComputerName/ComputerName").toString();
  moreinfo = "System information";
  moreinfo.append("\nComputerName::");
  moreinfo.append(computername);
  moreinfo.append("\nSystemManufacturer::");
  moreinfo.append(mname.value("Control/SystemInformation/SystemManufacturer").toString());
  moreinfo.append("\nProductName::");
  moreinfo.append(mname.value("Control/SystemInformation/SystemProductName").toString());
  #endif
  
  
  moreinfo.append("\nInterfaces_Id\n");
  for(i=0;i<ifacelist.size();i++) {
    moreinfo.append(ifacelist.at(i).name());
    moreinfo.append("::");
    moreinfo.append(ifacelist.at(i).hardwareAddress());
    moreinfo.append("\n");
  }
  QVariant idint = qChecksum((char*) moreinfo.constData(),moreinfo.size());
  moreinfo.append("\nChecksum::");
  moreinfo.append(idint.toString());
}

bool QLocatePCSetupData::loadatstartup(bool status = true, bool save = false) {
  #ifdef Q_WS_WIN
  QSettings sttup("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                  QSettings::NativeFormat);
  if(!status && save) {
    sttup.remove("QLocatePC");
    return (sttup.value("QLocatePC","")=="");
  } else if(status && save) {
    sttup.setValue("QLocatePC",QCoreApplication::arguments().at(0));
    return (sttup.value("QLocatePC","")!="");
  } else {
    return (sttup.value("QLocatePC","")!="");
  }
  #endif
  return false;
}

